package com.aptemo.webetek.client.widgets.teams;

import java.util.List;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.aptemo.webetek.client.widgets.HorizontalMenu;
import com.aptemo.webetek.client.widgets.dialogs.DecisionDialog;
import com.aptemo.webetek.client.widgets.dialogs.JoinTeamDialog;
import com.aptemo.webetek.client.widgets.links.EAnchor;
import com.aptemo.webetek.shared.LabelBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.HasOpenHandlers;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UserGroupRow
		extends Composite implements UserGroupRowPresenter.Display, ClickHandler
{
	private static UserGroupRowUiBinder	uiBinder	= GWT.create(UserGroupRowUiBinder.class);

	interface UserGroupRowUiBinder extends UiBinder<Widget, UserGroupRow>
	{
	}

	interface MyStyle extends CssResource
	{
		String isFavorite();

		String notFavorite();
	}

	@UiFactory
	protected DisclosurePanel makeDisclosurePanel(String header)
	{
		return new DisclosurePanel(header);
	}

	@UiField
	MyStyle					style;

	@UiField
	FocusPanel				panel;

	@UiField
	Label					title;

	@UiField
	Label					description;

	@UiField
	Anchor					joinLink;

	@UiField
	DisclosurePanel			tree;

	@UiField
	VerticalPanel			labels;

	@UiField
	Image					favouriteImage;

	private JoinTeamDialog	joinDialog;

	private DecisionDialog	deleteConfirmation	= new DecisionDialog("Deleting label...");

	private boolean			down				= false;

	public UserGroupRow()
	{
		initWidget(uiBinder.createAndBindUi(this));
		// Element el = getElement();
		// el.addClassName(bet.getStatus() == EnumBetStatus.ACTIVE ?
		// style.active() : style.inactive());
		// el.removeClassName(bet.getStatus() == EnumBetStatus.ACTIVE ?
		// style.inactive() : style.active());

		favouriteImage.setUrl("images/star_disabled.png");
	}

	@Override
	public HasText getNameField()
	{
		return title;
	}

	@Override
	public HasText getDescriptionField()
	{
		return description;
	}

	@Override
	public void setBetLabels(List<LabelBean> list, String teamId)
	{
		// tree.removeItems();
		labels.clear();
		if (list.size() > 0)
		{
			// tree.setHeader(new HTML("Tags..."));
			tree.setVisible(true);
			// TreeItem subtree = tree.addItem("Tags...");
			for (LabelBean item : list)
			{
				FlexTable row = new FlexTable();
				row.setWidth("100%");
				Hyperlink link = new Hyperlink(item.getTitle(), "group-" + teamId + "-" + item.getId());

				HorizontalMenu menu = new HorizontalMenu();
				EAnchor dellink = new EAnchor("x", item.getId());
				dellink.addClickHandler(this);

				// EAnchor editlink = new EAnchor("edit", item.getId());
				// menu.add(editlink);
				menu.add(dellink);

				row.setWidget(0, 0, link);
				row.setWidget(0, 1, menu);
				row.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT);

				// TreeItem ti = subtree.addItem(row);
				// ti.setUserObject(item);
				labels.add(row);
			}
			// tree.setContent(labels);
		}
	}

	@Override
	public String getClickedLink(ClickEvent event)
	{
		EAnchor a = (EAnchor) event.getSource();
		return a.getId();
	}

	@UiHandler("joinLink")
	void handleClick(ClickEvent e)
	{
		e.stopPropagation();
		joinDialog.center();
	}

	@Override
	public HasClickHandlers getPanelClick()
	{
		return panel;
	}

	@Override
	public HasDecisionHandlers getJoinDialogConfirmation()
	{
		if (joinDialog == null)
		{
			joinDialog = new JoinTeamDialog(title.getText());
		}
		return joinDialog;
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@UiHandler("favouriteImage")
	void handleFavouriteClick(ClickEvent e)
	{
		e.stopPropagation();
		down = !down;
		if (down)
		{
			favouriteImage.setUrl("images/star.png");
		}
		else
		{
			favouriteImage.setUrl("images/star_disabled.png");
		}
	}

	@Override
	public void hideJoinDialog()
	{
		joinDialog.hide();
	}

	@Override
	public HasDecisionHandlers getDeleteConfirmation()
	{
		return deleteConfirmation;
	}

	@Override
	public void onClick(ClickEvent event)
	{
		EAnchor link = (EAnchor) event.getSource();
		// if (event.getSource() == link.getText())
		deleteConfirmation.setId(link.getId());
		deleteConfirmation.setQuestionString(Webetek.getMessages().deleteTagQuestion(""));
		deleteConfirmation.center();
		event.stopPropagation();
	}

	@Override
	public void hideDeleteTagDialog()
	{
		deleteConfirmation.hide();
	}

	@Override
	public HasText getJoinNote()
	{
		return joinDialog.getJoinNoteField();
	}

	@Override
	public HasText getJoinSubject()
	{
		// TODO ...
		Label l = new Label(Webetek.getConstants().joinSubject());
		return l;
	}

	@Override
	public HasOpenHandlers<DisclosurePanel> getLabelTreeOpenHandler()
	{
		return tree;
	}
}
